﻿using System;
using System.IO;
using System.Text;

/*
namespace Sencha.Serialization
{
	public class JsonStream : IJsonSerializable
	{
		private string m_fileName;
		private Encoding m_writtenEncoding;
		private long m_bytesWritten;

		public string Filename { get { this.EnsureFile(); return m_fileName; } }
		public long Size { get { return m_bytesWritten; } }
		public Encoding Encoding { get { return m_writtenEncoding; } }

		#region IJsonSerializable Members

		public void ReadJson(IJsonReader reader, ISerializationContext context)
		{
			if (reader == null)
				throw new ArgumentNullException("reader");
			if (context == null)
				throw new ArgumentNullException("context");
			System.Diagnostics.Contracts.Contract.EndContractBlock();

			using (var fs = File.OpenWrite(this.Filename))
			{
				fs.Position = 0;

				m_writtenEncoding = context.Encoding;
				var writer = new StreamWriter(fs, context.Encoding);
				SkipValue(reader, writer);
				writer.Flush();
				m_bytesWritten = fs.Position;

				fs.SetLength(m_bytesWritten);
			}
		}

		private static bool SkipValue(IJsonReader reader, TextWriter buffer)
		{
			if (reader == null)
				throw new ArgumentNullException("reader");
			System.Diagnostics.Contracts.Contract.EndContractBlock();

			if (buffer != null && (reader.Token != JsonToken.None && reader.Token != JsonToken.EndOfStream))
				reader.TokenInfo.CopyJsonTo(buffer);

			switch (reader.Token)
			{
				case JsonToken.None:
				case JsonToken.EndOfStream:
				case JsonToken.DateTime:
				case JsonToken.ArrayEnd:
				case JsonToken.ObjectEnd:
				case JsonToken.Number:
				case JsonToken.Member:
				case JsonToken.StringLiteral:
				case JsonToken.Null:
					// returns True for primitives
					return reader.Token == JsonToken.Boolean || reader.Token == JsonToken.DateTime || reader.Token == JsonToken.Number || reader.Token == JsonToken.StringLiteral || reader.Token == JsonToken.Null;
				case JsonToken.ArrayBegin:
					reader.NextToken(); // advance

					while (reader.Token != JsonToken.ArrayEnd)
					{
						SkipValue(reader, buffer);
						reader.NextToken(); // skip array member, and advance
						if (reader.Token != JsonToken.ArrayEnd)
							buffer.Write(',');
					}

					SkipValue(reader, buffer); // skip ArrayEnd
					break;
				case JsonToken.ObjectBegin:
					reader.NextToken(); // advance
					while (reader.Token != JsonToken.ObjectEnd)
					{
						SkipValue(reader, buffer); reader.NextToken(); // skip member name, and advance
						buffer.Write(':');
						SkipValue(reader, buffer); reader.NextToken(); // skip member value, and advance
						if (reader.Token != JsonToken.ObjectEnd)
							buffer.Write(',');
					}
					SkipValue(reader, buffer); // skip ObjectEnd
					break;
			}

			return false;
		}

		public void WriteJson(IJsonWriter writer, ISerializationContext context)
		{
			if (writer == null)
				throw new ArgumentNullException("writer");
			if (context == null)
				throw new ArgumentNullException("context");
			System.Diagnostics.Contracts.Contract.EndContractBlock();

			if (m_bytesWritten == 0)
				return;

			using (var fs = File.OpenRead(this.Filename))
			{
				var encoding = m_writtenEncoding ?? context.Encoding;
				var reader = new StreamReader(fs, encoding, true, 1024);
				var buffer = new char[1024];
				var readed = 0;

				while ((readed = reader.Read(buffer, 0, buffer.Length)) > 0)
					writer.WriteJson(buffer, 0, readed);
			}
		}

		private void EnsureFile()
		{
			if (m_fileName == null)
				m_fileName = this.GetTemporaryFile();

			if (!File.Exists(m_fileName))
				throw new InvalidOperationException(string.Format("Temporary file '{0}' does not exists", m_fileName));
		}

		protected virtual string GetTemporaryFile()
		{
			return Path.GetTempFileName();
		}

		#endregion

		public override string ToString()
		{
			return m_fileName;
		}
	}
}
*/